Public Class Convert
    Public Sub ConvertToSheet(ByRef ExcelSheet As Microsoft.Office.Interop.Excel._Worksheet, ByVal FirstRange As String, ByVal Ranges As ConvertRange)
        Dim SheetRange As Microsoft.Office.Interop.Excel.Range
        With ExcelSheet
            SheetRange = .Range(FirstRange)
            SheetRange = SheetRange.Resize(Ranges.RowCount, Ranges.CoulmnCount)
            SheetRange.Value = Ranges.RangeObject
        End With
    End Sub

    Public Function ReadFromSheet(ExcelSheet As Microsoft.Office.Interop.Excel._Worksheet) As ConvertRange
        Dim uRanges As Microsoft.Office.Interop.Excel.Range = ExcelSheet.UsedRange
        Dim r As Int64 = ExcelSheet.Rows.Count
        Dim c As Int64 = ExcelSheet.Columns.Count
        Dim ur As Int64 = uRanges.Rows.Count
        Dim cr As Int64 = uRanges.Columns.Count
        Dim lr As Int64 = uRanges.Rows(ur).row
        Dim lc As Int64 = ExcelSheet.Columns(cr).column
        Dim rang As Microsoft.Office.Interop.Excel.Range = ExcelSheet.Range("A1")
        rang = rang.Resize(lr, lc)
        Dim rg As New ConvertRange
        rg.Cells = rang
        Return rg

    End Function
    Public Class ConvertRange
        Private _Cells(,) As Object
        Sub New()
            ReDim _Cells(0, 0)
        End Sub

        ReadOnly Property RangeObject() As Object
            Get
                Return _Cells
            End Get

        End Property
        ReadOnly Property RowCount() As Int64
            Get
                Return UBound(_Cells, 1) + 1
            End Get
        End Property
        ReadOnly Property CoulmnCount() As Int64
            Get
                Return UBound(_Cells, 2) + 1
            End Get
        End Property
        WriteOnly Property Cells() As Microsoft.Office.Interop.Excel.Range
            Set(value As Microsoft.Office.Interop.Excel.Range)
                _Cells = value.Value2
            End Set
        End Property

        Property Cells(row As Int64, column As Int64) As Object
            Get
                If row <= 0 Or column <= 0 Then
                    Return Nothing
                End If
                If UBound(_Cells, 1) >= row - 1 And UBound(_Cells, 2) >= column - 1 Then
                    Return _Cells(row - 1, column - 1)
                Else
                    Dim b(,) As Object
                    Dim r As Int64, c As Int64
                    If row - 1 > UBound(_Cells, 1) Then
                        r = row - 1
                    Else
                        r = UBound(_Cells, 1)
                    End If
                    If column - 1 > UBound(_Cells, 2) Then
                        c = column - 1
                    Else
                        c = UBound(_Cells, 2)
                    End If
                    ReDim b(r, c)
                    For i = 0 To UBound(_Cells, 1)
                        For j = 0 To UBound(_Cells, 2)
                            b(i, j) = _Cells(i, j)
                        Next
                    Next
                    Return b
                End If
            End Get
            Set(value As Object)
                If row <= 0 Or column <= 0 Then
                    Exit Property
                End If
                If UBound(_Cells, 1) >= row - 1 And UBound(_Cells, 2) >= column - 1 Then
                    _Cells(row - 1, column - 1) = value
                Else
                    Dim b(,) As Object
                    Dim r As Int64, c As Int64
                    If row - 1 > UBound(_Cells, 1) Then
                        r = row - 1
                    Else
                        r = UBound(_Cells, 1)
                    End If
                    If column - 1 > UBound(_Cells, 2) Then
                        c = column - 1
                    Else
                        c = UBound(_Cells, 2)
                    End If
                    ReDim b(r, c)
                    For i = 0 To UBound(_Cells, 1)
                        For j = 0 To UBound(_Cells, 2)
                            b(i, j) = _Cells(i, j)
                        Next
                    Next
                    _Cells = b
                    _Cells(row - 1, column - 1) = value
                End If
            End Set
        End Property


    End Class
End Class
